package com.jf.cloud.user.controller.app;

import com.jf.cloud.api.auth.feign.AuthSocialFeignClient;
import com.jf.cloud.user.model.UserTravelsComment;
import com.jf.cloud.user.model.UserTravelsCommentLog;
import com.jf.cloud.user.service.UserService;
import com.jf.cloud.user.service.UserTravelsCommentLogService;
import com.jf.cloud.user.vo.UserTravelsCommentLogVO;
import com.jf.cloud.user.dto.UserTravelsCommentLogDTO;

import com.jf.cloud.api.auth.bo.UserInfoInTokenBO;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

/**
 * 
 *
 * @author zz
 * @date 2023-12-01 15:16:03
 */
@RestController("appUserTravelsCommentLogController")
@RequestMapping("/user_travels_comment_log")
@Tag(name = "回复评论")
public class UserTravelsCommentLogController {

    @Autowired
    private UserTravelsCommentLogService userTravelsCommentLogService;
    @Autowired
    private AuthSocialFeignClient authSocialFeignClient;

    @Autowired
    private UserService userService;

    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("/page")
    @Operation(summary = "获取列表", description = "分页获取列表")
    public ServerResponseEntity<PageVO<UserTravelsCommentLog>> page(@Valid PageDTO pageDTO, @ParameterObject UserTravelsCommentLog userTravelsCommentLog) {
        if(userTravelsCommentLog.getComId()==null){
            return ServerResponseEntity.showFailMsg("参数不能为空");
        }
        PageVO<UserTravelsCommentLog> userTravelsCommentLogPage = userTravelsCommentLogService.page(pageDTO,userTravelsCommentLog.getComId());
        //TODO  继续获取用户头像昵称
        List<UserTravelsCommentLog> list=userTravelsCommentLogPage.getList();
        for (int i = 0; i < list.size(); i++) {
            UserTravelsCommentLog userTravelsComment=list.get(i);
            userTravelsComment.setNickName(userService.getById(userTravelsComment.getUserId()).getNickName());
            userTravelsComment.setImgUrl(userService.getByUserId(userTravelsComment.getUserId()).getPic());
        }
        return ServerResponseEntity.success(userTravelsCommentLogPage);
    }

    @GetMapping
    @Operation(summary = "获取", description = "根据id获取")
    public ServerResponseEntity<UserTravelsCommentLog> getById(@RequestParam Long id) {
        return ServerResponseEntity.success(userTravelsCommentLogService.getById(id));
    }

    @GetMapping("/count")
    @Operation(summary = "根据评论记录ID获取总数", description = "根据评论记录ID获取总数")
    public ServerResponseEntity<Integer> count(@RequestParam Long comId) {
        return ServerResponseEntity.success(userTravelsCommentLogService.count(comId));
    }

    @PostMapping
    @Operation(summary = "保存", description = "保存")
    public ServerResponseEntity<Void> save(@Valid @RequestBody UserTravelsCommentLogDTO userTravelsCommentLogDTO) {
        userTravelsCommentLogDTO.setUserId(AuthUserContext.get().getUserId());
        UserTravelsCommentLog userTravelsCommentLog = mapperFacade.map(userTravelsCommentLogDTO, UserTravelsCommentLog.class);
        userTravelsCommentLogService.save(userTravelsCommentLog);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新", description = "更新")
    public ServerResponseEntity<Void> update(@Valid @RequestBody UserTravelsCommentLogDTO userTravelsCommentLogDTO) {
        UserTravelsCommentLog userTravelsCommentLog = mapperFacade.map(userTravelsCommentLogDTO, UserTravelsCommentLog.class);
        userTravelsCommentLogService.update(userTravelsCommentLog);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除", description = "根据id删除")
    public ServerResponseEntity<Void> delete(@RequestParam Long id) {
        userTravelsCommentLogService.deleteById(id);
        return ServerResponseEntity.success();
    }
}
